import { createRouter, createWebHistory } from 'vue-router'
import { h, resolveComponent } from 'vue'
const router = createRouter({
    history: createWebHistory('/'),
    routes: [
        {
            path: '/',
            component: () => import('../views/layout.vue'),
            children: [
                {
                    path: '/',
                    name: 'home',
                    component: () => import('../views/home/index.vue'),
                },
                {
                    path: '/cart',
                    name: 'cart',
                    component: () => import('../views/cart/index.vue'),
                },
                {
                    path: '/order',
                    name: 'order',
                    component: () => import('../views/order/index.vue'),
                },
                {
                    path: '/payment',
                    name: 'payment',
                    component: () => import('../views/payment/index.vue'),
                },
                {
                    path: '/category/:pid',
                    name: 'category',
                    component: () => import('../views/category/index.vue'),
                },
                {
                    path: '/category/sub/:cid',
                    name: 'sub',
                    component: () => import('../views/category/sub.vue'),
                },
                {
                    path: '/goods/:gid',
                    name: 'goods',
                    component: () => import('../views/goods/index.vue'),
                },
                {
                    // 我的账户模块
                    path: '/member',
                    name: 'member',
                    component: () => import('../views/member/index.vue'),

                    children: [
                        {
                            // 个人中心
                            path: '/member',
                            component: () =>
                                import('../views/member/home/index.vue'),
                        },
                        {
                            path: '/member/order',
                            component: {
                                render: () =>
                                    h(resolveComponent('router-view')),
                            },
                            children: [
                                {
                                    path: '',
                                    component: () =>
                                        import(
                                            '../views/member/components/member-order.vue'
                                        ),
                                },
                            ],
                        },
                        {
                            // 个人信息
                            path: '/member/info',
                            component: {
                                render: () =>
                                    h(resolveComponent('router-view')),
                            },

                            children: [
                                {
                                    path: '',
                                    component: () =>
                                        import(
                                            '../views/member/components/member-info.vue'
                                        ),
                                },
                            ],
                        },
                        {
                            // 消息通知
                            path: '/member/message',
                            component: {
                                render: () =>
                                    h(resolveComponent('router-view')),
                            },
                            children: [
                                {
                                    path: '',
                                    component: () =>
                                        import(
                                            '../views/member/components/member-message.vue'
                                        ),
                                },
                            ],
                        },
                        {
                            // 安全设置
                            path: '/member/securitySett',
                            component: {
                                render: () =>
                                    h(resolveComponent('router-view')),
                            },
                            children: [
                                {
                                    path: '',
                                    component: () =>
                                        import(
                                            '../views/member/components/member-security-sett.vue'
                                        ),
                                },
                            ],
                        },
                        {
                            // 地址管理
                            path: '/member/address',
                            component: {
                                render: () =>
                                    h(resolveComponent('router-view')),
                            },
                            children: [
                                {
                                    path: '',
                                    component: () =>
                                        import(
                                            '../views/member/components/member-address.vue'
                                        ),
                                },
                            ],
                        },
                        {
                            // 我的积分
                            path: '/member/my-points',
                            component: {
                                render: () =>
                                    h(resolveComponent('router-view')),
                            },
                            children: [
                                {
                                    path: '',
                                    component: () =>
                                        import(
                                            '../views/member/components/member-my-points.vue'
                                        ),
                                },
                            ],
                        },
                        {
                            // 邀请有礼
                            path: '/member/invite-courteous',
                            component: {
                                render: () =>
                                    h(resolveComponent('router-view')),
                            },
                            children: [
                                {
                                    path: '',
                                    component: () =>
                                        import(
                                            '../views/member/components/member-invite-courteous.vue'
                                        ),
                                },
                            ],
                        },
                        {
                            // 我的足迹
                            path: '/member/my-tracks',
                            component: {
                                render: () =>
                                    h(resolveComponent('router-view')),
                            },
                            children: [
                                {
                                    path: '',
                                    component: () =>
                                        import(
                                            '../views/member/components/member-my-tracks.vue'
                                        ),
                                },
                            ],
                        },
                        {
                            // 幸运抽奖
                            path: '/member/lucky-draw',
                            component: {
                                render: () =>
                                    h(resolveComponent('router-view')),
                            },
                            children: [
                                {
                                    path: '',
                                    component: () =>
                                        import(
                                            '../views/member/components/member-lucky-draw.vue'
                                        ),
                                },
                            ],
                        },
                        {
                            // 优惠券
                            path: '/member/coupon',
                            component: {
                                render: () =>
                                    h(resolveComponent('router-view')),
                            },
                            children: [
                                {
                                    path: '',
                                    component: () =>
                                        import(
                                            '../views/member/components/member-coupon.vue'
                                        ),
                                },
                            ],
                        },
                        {
                            // 礼品卡
                            path: '/member/gift-card',
                            component: {
                                render: () =>
                                    h(resolveComponent('router-view')),
                            },
                            children: [
                                {
                                    path: '',
                                    component: () =>
                                        import(
                                            '../views/member/components/member-gift-card.vue'
                                        ),
                                },
                            ],
                        },
                        {
                            // 礼品卡
                            path: '/member/evaluate-sun-exposure',
                            component: {
                                render: () =>
                                    h(resolveComponent('router-view')),
                            },
                            children: [
                                {
                                    path: '',
                                    component: () =>
                                        import(
                                            '../views/member/components/member-evaluate-sun-exposure.vue'
                                        ),
                                },
                            ],
                        },
                        {
                            // 售后服务
                            path: '/member/after-sale-service',
                            component: {
                                render: () =>
                                    h(resolveComponent('router-view')),
                            },
                            children: [
                                {
                                    path: '',
                                    component: () =>
                                        import(
                                            '../views/member/components/member-after-sale-service.vue'
                                        ),
                                },
                            ],
                        },
                        {
                            // 帮助中心
                            path: '/member/help-center',
                            component: {
                                render: () =>
                                    h(resolveComponent('router-view')),
                            },
                            children: [
                                {
                                    path: '',
                                    component: () =>
                                        import(
                                            '../views/member/components/member-help-center.vue'
                                        ),
                                },
                            ],
                        },
                        {
                            // 在线服务
                            path: '/member/online-service',
                            component: {
                                render: () =>
                                    h(resolveComponent('router-view')),
                            },
                            children: [
                                {
                                    path: '',
                                    component: () =>
                                        import(
                                            '../views/member/components/member-online-service.vue'
                                        ),
                                },
                            ],
                        },
                    ],
                    beforeEnter(to, from, next) {
                        try {
                            if (
                                JSON.parse(localStorage.useManage).profile.token
                            ) {
                                next()
                            } else {
                                router.push('/login')
                            }
                        } catch (error) {
                            router.push('/login')
                        }
                    },
                },
                {
                    path: '/search',
                    component: () => import('../views/search/index.vue'),
                },
                {
                    path: '/404',
                    name: 'notFound',
                    component: () => import('../views/404.vue'),
                },
            ],
        },
        {
            path: '/login',
            name: 'login',
            component: () => import('../views/login/index.vue'),
            children: [],
        },
        // 所有未定义路由，全部重定向到404页
        {
            path: '/:any(.*)',
            redirect: '/404',
        },
    ],
    scrollBehavior() {
        // 延时滚动
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve({ left: 0, top: 0, behavior: 'smooth' })
            }, 100)
        })
    },
})

export default router
